<?php
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_SESSION['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if($_RQ['mid']){
		$params['mid']=intval($_RQ['mid']);
		$return['params']['mid']=$_RQ['mid'];
	}
	//创建时间
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
		$return['params']['createtime']=$time_range;
	}
	$psize=20;
	$pindex=intval($_RQ['page'])?:1;
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,$psize),
		'orderby'=>'createtime desc',
		
	);
	$list=$this->get_list('merch',$parameter,$total);
	if($list){
		foreach($list as &$item){
			$item['member']=pdo_get('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['mid']),array('from_os','nickname','avatar','mobile','sex','pcd'));
			$item['from_os']=$item['member']['from_os']?get_ostitle()[$item['member']['from_os']]:'微信';
			$item['sex']=$item['member']['sex']==1?'男':'女';
			$pcd=$item['member']['pcd']?json_decode($item['member']['pcd'],true):array();
			$item['pcd']=$pcd['province'].' '.$pcd['city'];
			$item['agent']=cfc('core')->member_show($item['agent_id'],'',array('nickname'=>'总店'));
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		//'export'=>'export',
		'tabs'=>array(
			array('op'=>'list','title'=>'商户管理'),
			array('op'=>'edit','title'=>'添加商户'),
		),
		'list'=>array('data'=>'list','tip'=>'<red>[温馨提示]：关于匿名用户的解释：正常认证服务号，没有关闭获取粉丝详细的情况下，是不会存在匿名用户的；可能小程序出现匿名用户的情况居多：原因是：小程序官方不允许首次访问弹窗获取粉丝详细。但是为了上下级的准确性，在访问小程序首页的时候就产生了下级关系！这时候就会出现匿名用户的情况。但是只要这个匿名用户访问了购物车，商品，会员中心等任何页面。就会弹窗获取粉丝信息。这时候匿名用户就会变成带昵称头像的会员了！</red>','del_one'=>true,'del_message'=>'确定删除分商商吗？会同时解除其下所有上下级关系哦！','delall'=>false,'list'=>array(
			array('name'=>'id','title'=>'ID','type'=>'string'),
			array('name'=>'member','title'=>'用户','type'=>'member'),
			array('name'=>'agent','title'=>'推荐人','type'=>'member'),
			array('name'=>'from_os','title'=>'来源','type'=>'string'),
			array('name'=>'pcd','title'=>'区域','type'=>'string'),
			array('name'=>'sex','title'=>'性别','type'=>'string'),
			array('name'=>'diannum','title'=>'点数','type'=>'string'),
			array('name'=>'endtime','title'=>'过期时间','type'=>'time'),
		),'action'=>array(
			array('type'=>'href','action'=>'/game/','title'=>'他的活动'),
		)),
		'search'=>array(
			array('type'=>'choose','width'=>15,'action'=>'core/member.get_member/','name'=>'mid','title'=>'选择会员'),
			array('name'=>'createtime','width'=>15,'title'=>'加入时间','type'=>'time_range'),
		),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=$this->pdo_get('merch',array('id'=>$id));
	}else{
		$item=array();
	}
	$item['endtime'] =$item['endtime']?date('Y/m/d H:i',$item['endtime']):'';
	$item['upass']='';
    $return['item']=$item;
	$htmls=array( 
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'商户设置',
		'tabs'=>array(
			array('op'=>'list','title'=>'商户管理'),
			array('op'=>'edit','title'=>'添加商户'),
		),
		'edit'=>array(
			array('name'=>'title','must'=>true,'title'=>'商户名称','type'=>'string'),
			array('name'=>'logo','title'=>'商户logo','type'=>'image'),
			array('name'=>'agent_id','title'=>'绑定代理商','type'=>'choose','action'=>'get_merch'),
			array('name'=>'mid','title'=>'绑定管理员','type'=>'choose','action'=>'core/member.get_member/'),
			array('type'=>'string','title'=>'昵称','name'=>'nickname'),
			array('name'=>'realname','title'=>'真实姓名','type'=>'string'),
			array('name'=>'mobile','title'=>'手机号','type'=>'string'),
			array('name'=>'upass','title'=>'账户密码','type'=>'string'),
			array('name'=>'sex','title'=>'性别','type'=>'radio','options'=>'sex'),
			array('name'=>'gamenum','title'=>'活动数量','type'=>'unitstring','unit'=>'个'),	
			array('name'=>'diannum','title'=>'点数','type'=>'unitstring','unit'=>'点'),	
			array('type'=>'time','title'=>'到期时间','name'=>'endtime'),
		)
	);
	$return['status']=array('0'=>'待审核','1'=>'入驻中','2'=>'暂停中');
	$return['sex']=array('1'=>'男','2'=>'女');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$post=get_postdata();
	if(!$post['title']){
		exi('商户名称必填','error');
	}
	if(!$post['mid']){
		exi('商户必须绑定管理员ID','error');
	}
	$data =array(
		'upass'=>base64_encode($post['upass']),
		'mobile'=>$post['mobile'],
		'realname'=>$post['realname'],
		'title'=>$post['title'],
		'endtime'=>strtotime($post['endtime']),
		'logo'=>$post['logo'],
		'sex'=>intval($post['sex']),
		'mid'=>intval($post['mid']),
		'agent_id'=>intval($post['agent_id']),
		'nickname'=>$post['nickname'],
		'mobile'=>$post['mobile'],
		'realname'=>$post['realname'],
		'gamenum'=>intval($post['gamenum']),
		'diannum'=>intval($post['diannum']),
	);
	if($post['id']){
		if(!$post['upass']){
			unset($data['upass']);
		}
		$this->pdo_update('merch',$data,array('id'=>$post['id']));
		exi('保存成功','','list');
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['createtime']=time();
		$this->pdo_insert('merch',$data);
		exi('添加成功','','list');
	}
	
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请选择商户！！','error');
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				$this->pdo_delete('merch',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
			}
		}
	}
	exi('删除成功！');
}
?>